package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public int createOrders(String userId, int businessId, double orderTotal, int daId) {
        int orderid=0;
        try {
            DBUtil.beginTransaction();         //开启一个事务
            //调用多个Dao进行数据处理
            OrdersDao orderDao = new OrdersDaoImpl();
            Cart cart = new Cart();
            //1.根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录
            // 并获取自动生成的订单编号
            orderid=orderDao.createOrders(userId,businessId,orderTotal,daId);
            CartDao cartDao = new CartDaoImpl();
            //2.根据用户编号、商家编号从购物车表中查询所有数据
            List<Cart> carts =cartDao.listCart(cart);
            List<Orderdetailet> list =new ArrayList<>();

            //3.批量添加到订单明细表中
            OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
            for (int i = 0; i <carts.size(); i++) {
                Orderdetailet orderdetailet = new Orderdetailet();
                orderdetailet.setOrderId(orderid);
                orderdetailet.setFoodId(carts.get(i).getFoodId());
                orderdetailet.setQuantity(carts.get(i).getQuantity());
                list.add(orderdetailet);
                orderdetailetDao.saveOrderDetailet(list);
                //4.根据用户编号、商家编号删除购物车表中的数据
                cartDao.removeCart(cart);
            }
            //3.批量添加到订单明细表中
//            OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
//            orderdetailetDao.saveOrderDetailet(list);

            DBUtil.commitTransaction();        //提交一个事务

        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return orderid;
        }

    @Override
    public Orders getOrdersById(int orderId) {
        OrdersDaoImpl ordersDao = new OrdersDaoImpl();
        Orders orders = new Orders();
        try {
            DBUtil.getConnection();
            orders = ordersDao.getOrdersById(orderId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        OrdersDaoImpl ordersDao = new OrdersDaoImpl();
        List<Orders> arrayList = new ArrayList<>();
        try {
            DBUtil.getConnection();
            arrayList = ordersDao.listOrdersByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return arrayList;
    }
}
